package com.office.common.utils.email;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件发送工具类（支持动态 from 和 to）
 *
 * @author: 柒月
 * @date: 2025/5/30 下午9:23
 * @version: 1.0
 */
@Component
public class EmailUtil {

    // 记录日志
    private static final Logger log = LoggerFactory.getLogger(EmailUtil.class);

    private final JavaMailSender javaMailSender;

    @Autowired
    public EmailUtil(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    /**
     * 发送简单文本邮件
     *
     * @param from    发件人邮箱
     * @param to      收件人邮箱
     * @param subject 邮件主题
     * @param text    邮件正文内容
     */
    public void sendSimpleEmail(String from, String to, String subject, String text) {
        log.info("发送简单文本邮件主题 {}", subject);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);
    }

    /**
     * 发送 HTML 格式邮件
     *
     * @param from        发件人邮箱
     * @param to          收件人邮箱
     * @param subject     邮件主题
     * @param htmlContent HTML 内容
     * @throws MessagingException 构建失败抛出异常
     */
    public void sendHtmlEmail(String from, String to, String subject, String htmlContent) throws MessagingException {
        log.info("发送 HTML 格式邮件 {}", subject);
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(htmlContent, true); // true 表示 HTML
        javaMailSender.send(message);
    }

    /**
     * 发送带附件的邮件
     *
     * @param from           发件人邮箱
     * @param to             收件人邮箱
     * @param subject        邮件主题
     * @param text           正文内容
     * @param attachmentPath 附件路径
     * @throws MessagingException 构建失败抛出异常
     */
    public void sendEmailWithAttachment(String from, String to, String subject, String text, String attachmentPath) throws MessagingException {
        log.info("发送带附件的邮件 {},", subject);
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);
        FileSystemResource file = new FileSystemResource(new File(attachmentPath));
        helper.addAttachment(file.getFilename(), file);
        javaMailSender.send(message);
    }
}
